//
//  BlackScroller.m
//  ABlip
//
//  Created by dawid on 09-01-15.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "BlackScroller.h"
#define BACKGROUND_COLOR 0.2392

@implementation BlackScroller
- (void)drawRect:(NSRect)rect
{
	

	[self drawKnobSlot];
	[self drawKnob];
	
}
-(void)awakeFromNib
{
	[self setControlTint:NSClearControlTint];
}
- (void)drawKnob
{

	NSRect position = [self rectForPart:NSScrollerNoPart];
	
	//[[NSColor colorWithCalibratedRed:255 green:255 blue:255 alpha:0.0] set];
	//[NSBezierPath fillRect:position];
	if (position.size.height < 1){
		return;
	}
	 position = [self rectForPart:NSScrollerKnob];

	if (position.size.height < 1){
		return;
	}
	if (position.size.height <= 20){

		position.size.height  =20;
	}
	

	float width  = 20 ;//position.size.width;
	float offset = -1;
	
	NSSize topImgSize = [BlackScroller topSlider].size;

	[[BlackScroller topSlider] drawInRect:NSMakeRect(position.origin.x+offset, position.origin.y,  
								  width, topImgSize.height)
			  fromRect:NSMakeRect(0, 0, topImgSize.width, topImgSize.height)
			 operation:NSCompositeSourceOver
			  fraction:1.0];
	
	NSSize bottomImgSize = [BlackScroller bottomSlider].size;
	[[BlackScroller bottomSlider] drawInRect:NSMakeRect(position.origin.x+offset,position.origin.y+ (position.size.height-bottomImgSize.height), 
									width, bottomImgSize.height)
				 fromRect:NSMakeRect(0, 0, 
									 bottomImgSize.width,  bottomImgSize.height)
				operation:NSCompositeSourceOver
				 fraction:1.0];


	NSSize centerImgSize = [BlackScroller middleSlider].size;
	
	[[BlackScroller middleSlider] drawInRect:NSMakeRect(position.origin.x+offset, position.origin.y + topImgSize.height,  
										 width, position.size.height - bottomImgSize.height - topImgSize.height )
					 fromRect:NSMakeRect(0, 0, centerImgSize.width,  
										 centerImgSize.height)
					operation:NSCompositeSourceOver
					 fraction:1.0];

					 
	
}

- (void)drawKnobSlot
{
	
		//NSRect position = [self rectForPart:NSScrollerNoPart];
	NSRect position = [self rectForPart:NSScrollerKnobSlot];
	NSRect noScrollPart = [self rectForPart:NSScrollerNoPart];
	[[NSColor colorWithCalibratedRed:BACKGROUND_COLOR green:BACKGROUND_COLOR blue:BACKGROUND_COLOR alpha:1.0] set];
//	[[NSColor clearColor] set];
//	[NSColor whiteColor];
	[NSBezierPath fillRect:noScrollPart];
	[NSBezierPath fillRect:position];
//	[[NSColor colorWithCalibratedRed:255 green:255 blue:255 alpha:0.0] set];
//	[NSBezierPath fillRect:position];
	//	return;
	if (position.size.height < 1){
		return;
	}
	
	float offset = 0;//-4;
	float xOffset = -2;

	float width  = 22;//position.size.width;
	

	NSSize topImgSize = [BlackScroller topTrack].size;

		
	[[BlackScroller topTrack] drawInRect:NSMakeRect(position.origin.x + xOffset, position.origin.y - offset,  
							  width, topImgSize.height)
			  fromRect:NSMakeRect(0, 0, topImgSize.width, topImgSize.height)
			 operation:NSCompositeSourceOver
			  fraction:1.0];
	
	int offset2 =0;// -4;
	NSSize bottomImgSize = [BlackScroller bottomTrack].size;
	[[BlackScroller bottomTrack] drawInRect:NSMakeRect(position.origin.x+ xOffset,position.origin.y +offset2+(position.size.height-bottomImgSize.height), 
									 width, bottomImgSize.height)
				 fromRect:NSMakeRect(0, 0, 
									 bottomImgSize.width,  bottomImgSize.height)
				operation:NSCompositeSourceOver
				 fraction:1.0];

		NSSize centerImgSize = [BlackScroller middleTrack].size;
		
		

		[[BlackScroller middleTrack] drawInRect:NSMakeRect(position.origin.x+ xOffset, position.origin.y + topImgSize.height - offset,  
										 width, position.size.height - bottomImgSize.height - topImgSize.height  + offset + offset)
					 fromRect:NSMakeRect(0, 0, centerImgSize.width,  
										 centerImgSize.height)
					operation:NSCompositeSourceOver
					 fraction:1.0];

	
	return;
}

+(NSImage*)middleSlider
{
	static NSImage * image = nil;
	if (image==nil){
		NSBundle *bundle = [NSBundle mainBundle];
		image = [[NSImage alloc] initWithContentsOfFile: [bundle pathForResource:@"sliderMiddle" ofType:@"png" ]];
		[image setFlipped:YES];

	}
	return image;
}
+(NSImage*)topSlider
{
	static NSImage * image = nil;
	if (image==nil){
		NSBundle *bundle = [NSBundle mainBundle];
		image = [[NSImage alloc] initWithContentsOfFile: [bundle pathForResource:@"sliderTop" ofType:@"png" ]];
		[image setFlipped:YES];
	}
	return image;
}
+(NSImage*)bottomSlider
{
	static NSImage * image = nil;
	if (image==nil){
		NSBundle *bundle = [NSBundle mainBundle];
		image = [[NSImage alloc] initWithContentsOfFile: [bundle pathForResource:@"sliderBottom" ofType:@"png" ]];
		[image setFlipped:YES];
	}
	return image;
}
+(NSImage*)middleTrack
{
	static NSImage * image = nil;
	if (image==nil){
		NSBundle *bundle = [NSBundle mainBundle];
		image = [[NSImage alloc] initWithContentsOfFile: [bundle pathForResource:@"trackMiddle" ofType:@"png" ]];
		[image setFlipped:YES];
		
		
	}
	return image;
}
+(NSImage*)topTrack
{
	static NSImage * image = nil;
	if (image==nil){
		NSBundle *bundle = [NSBundle mainBundle];
		image = [[NSImage alloc] initWithContentsOfFile: [bundle pathForResource:@"trackTop" ofType:@"png" ]];
		[image setFlipped:YES];
	}
	return image;
}
+(NSImage*)bottomTrack
{
	static NSImage * image = nil;
	if (image==nil){
		NSBundle *bundle = [NSBundle mainBundle];
		image = [[NSImage alloc] initWithContentsOfFile: [bundle pathForResource:@"trackBottom" ofType:@"png" ]];
		[image setFlipped:YES];
	}
	return image;
}
@end
